<?php

namespace app\admin\validate\shop\goods;

use app\shop\library\ShopHelper;
use think\Validate;

class Classify extends Validate
{
    protected $failException = true;

    /**
     * 验证规则
     */
    protected $rule = [
        'status' => 'checkStatus',
        'name'   => 'require'
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'name.require'       => '分类名称不能为空',
        'status.checkStatus' => '分类状态不能为空'
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];

    protected function checkStatus($value, $rule, $data = []): bool
    {
        if (count($data) === 2 && isset($data['id']) && isset($data['status'])) {
            $this->only(['status']);
            return !ShopHelper::is_empty($data['status']);
        }
        return true;
    }
}
